/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ugame.utility;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author user
 */
public class Security {

    private static Security instance;

    public static Security getInstance() {
        if (instance == null) {
            instance = new Security();
        }
        return instance;
    }

    public String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            
            // Now we need to zero pad it if you actually want the full 32 chars.
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException ex) {
        }
        return null;
    }

    public String getPasswordMD5(String firstInput, String secondInput){
        String usernameMD5 = getMD5(firstInput);
        String passwordMD5 = getMD5(secondInput);
        String temp = usernameMD5.substring(0, usernameMD5.length() / 2) + passwordMD5.substring(0, passwordMD5.length() / 2);
        return getMD5(temp);
    }
}
